﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using HCI.Questionaire.Entities;

namespace HCI.Questionaire.QuestionRender
{
    public partial class QuestionGroup3Render : UserControl, IQuestionGroupRenderControl
    {
        private StackPanel _currentFieldItem;

        public QuestionGroup3Render()
        {
            InitializeComponent();
        }

        #region IQuestionGroupRenderControl Members

        public void InitailRender(HCI.Questionaire.QuestionGroup.IQuestionGroup questionGroup)
        {

            this.LayoutRoot.RowDefinitions[0].Height = questionGroup.CurrentQuestionIndex == 0 ? new GridLength(48) : new GridLength(80);
            this.DataContext = new RetingQuestionInfo
            {
                QuestionText = ((RatingQuestionGroup)questionGroup.QuestionList[questionGroup.CurrentQuestionIndex]).QuestionText,
                FirstPageVisible = questionGroup.CurrentQuestionIndex == 0 ? Visibility.Visible : Visibility.Collapsed,
                SecoundPageVisible = questionGroup.CurrentQuestionIndex == 1 ? Visibility.Visible : Visibility.Collapsed,
                Description = "What WHAT wHaT??",
                QuestionCollection = ((RatingQuestionGroup)questionGroup.QuestionList[questionGroup.CurrentQuestionIndex]).QuestionList

            };
        }

        public string QuestionRunningNumber
        {
            get
            {
                return this.txtQuestionRunningNumber.Text;
            }
            set
            {
                this.txtQuestionRunningNumber.Text = value;
            }
        }
        #endregion

        private void StackPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this._currentFieldItem != null)
            {
                ((Border)this._currentFieldItem.Children[0]).BorderBrush = new SolidColorBrush(Color.FromArgb(255, 204, 204, 204));
                ((Border)((StackPanel)sender).Children[0]).BorderThickness = new Thickness(1.0);
            }
            ((Border)((StackPanel)sender).Children[0]).BorderBrush = this.fieldsBorderBrush;
            ((Border)((StackPanel)sender).Children[0]).BorderThickness = new Thickness(1.5);
            this._currentFieldItem = (StackPanel)sender;

            // TODO: Reder Checked image here
            // TODO: How to set selected to the answer and bind data again?
        }

        private void _field_MouseEnter(object sender, MouseEventArgs e)
        {
            ((Border)sender).BorderBrush = this.fieldsBorderBrush;
            ((Border)sender).BorderThickness = new Thickness(1.5);

            this.fieldMouseOver.Stop();
            Storyboard.SetTarget(this.fieldMouseOver, (DependencyObject)sender);
            this.fieldMouseOver.Begin();
        }

        private void _field_MouseLeave(object sender, MouseEventArgs e)
        {
            if (this._currentFieldItem != null && ((Border)((StackPanel)this._currentFieldItem).Children[0]) == ((Border)sender))
            {
                return;
            }

            ((Border)sender).BorderBrush = new SolidColorBrush(Colors.Gray);
            ((Border)sender).BorderThickness = new Thickness(1.5);
            this.fieldMouseOut.Stop();
            Storyboard.SetTarget(this.fieldMouseOut, (DependencyObject)sender);
            this.fieldMouseOut.Begin();

        }

        public class RetingQuestionInfo
        {
            
            public string QuestionText { get; set; }
            public string Description { get; set; }
            public List<RatingQuestion> QuestionCollection { get; set; }
            public Visibility FirstPageVisible { get; set; }
            public Visibility SecoundPageVisible { get; set; }
        }

        private void TextBox_KeyUp(object sender, KeyEventArgs e)
        {

        }

        private TextBox txtSecoundAnswer;
        private void TextBox_Loaded(object sender, RoutedEventArgs e)
        {
            this.txtSecoundAnswer = sender as TextBox;

            this.Dispatcher.BeginInvoke(delegate { this.txtSecoundAnswer.Focus(); });
        }
    }
}
